﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 反射1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            Person p2 = new Person();
            Person p3 = new Person();

            //下面有n种获取Person对应的Type的方法
            //最终都是获得的同一个对象，因为一个类就对应一个对象
            Type t1 = p1.GetType();
            Type t2 = p2.GetType();
            Type t3 = typeof(Person);
            Type t4 = Type.GetType("反射1.Person");

            Console.WriteLine(t1);
            Console.WriteLine(t2);
            Console.WriteLine(t3);
            Console.WriteLine(t4);

            //他们都返回一个对象
            Console.WriteLine(Object.ReferenceEquals(t1,t2));
            Console.WriteLine(Object.ReferenceEquals(t2,t3));
            Console.WriteLine(Object.ReferenceEquals(t3,t4));

            //Activator.CreateInstance(t1),动态创建类，类必须有无参的构造函数
            object obj = Activator.CreateInstance(t1);//动态创建t1指向的类的对象 new Person()
            Person p = (Person)obj;

            Console.WriteLine(obj);


            Child1 c1 = new Child1();
            c1.Hello();//结果：反射1.Child1

            //c1.Test();

            Console.ReadKey();


        }


    }

    class Person //一个类对应一个Type对象
    {
        //快捷键 输入override 再空格输入to，按照提示选择ToString(),自动生成重载的代码
        public override string ToString()
        {
            return "我是person";
        }
    }

    class Parent
    {
        public void Hello()
        {
            //Type type = this.GetType();//this不是当前类，而是当前对象，this看多当前对象的一个特殊变量
            //Console.WriteLine(type);

            this.Test();

           
        } 

        public virtual void Test()
        {
            Console.WriteLine("Parent Test");
        }
    }

    class Child1:Parent
    {
        public override void Test()
        {
            Console.WriteLine("Child1 Test");
        }
    }

    class Child2 : Parent
    {

    }


}
